﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;
using UnityEngine;
using Verse.AI;

namespace Miao
{
    [StaticConstructorOnStartup]
    public class MultiplePawn : Pawn
    {
        //public GraphicData DraftedGraph;
        //public GraphicData graph;

        //public Graphic DraftedGraph;
        public Graphic DraftedGraph
        {
            get
            {
                return this.def.GetModExtension<MultipleGraphDefModExtension>().graphicData.Graphic;
            }
        }

        public Graphic Graph;

        public MultiplePawn()
        {


        }
        public override void ExposeData()
        {
            base.ExposeData();
            //if(Scribe.mode== LoadSaveMode.ResolvingCrossRefs)
            //{
            //    this.def.GetModExtension<MultipleGraphDefModExtension>().graphicData.ExplicitlyInitCachedGraphic();
            //}
        }

        public override void SpawnSetup(Map map, bool respawningAfterLoad)
        {
            base.SpawnSetup(map, respawningAfterLoad);
            //DraftedGraph = this.def.GetModExtension<MultipleGraphDefModExtension>().graphicData.Graphic;
            Graph = this.kindDef.lifeStages[0].bodyGraphicData.Graphic;
            //Graph = this.Drawer.renderer.graphics.nakedGraphic;
        }

        public override void Tick()
        {
            base.Tick();
        }
        bool IsChange;

        public override void DynamicDrawPhaseAt(DrawPhase phase, Vector3 drawLoc, bool flip = false)
        {
            //base.DynamicDrawPhaseAt(phase, drawLoc, flip);
            if (phase == DrawPhase.Draw)
            {
                DrawAt(drawLoc, flip);
            }
        }


        protected override void DrawAt(Vector3 drawLoc, bool flip = false)//定义一种PawnRenderNodeTagDefOf来绑定
        {


            if (!Drafted)
            {
                this.Drawer.renderer.BodyGraphic.Draw(drawLoc, Rotation, this);
                //this.Drawer.renderer.graphics.nakedGraphic = Graph;
            }
            else
            {
                DraftedGraph.Draw(drawLoc, flip ? Rotation.Opposite : Rotation, this);
                //rotationTracker.UpdateRotation();
                //this.Drawer.renderer.graphics.nakedGraphic = DraftedGraph;
                //Mesh mesh = Graph.graphicData.Graphic.MeshAt(this.Rotation);
                //Vector3 drawPos = DrawPos;
                //drawPos.y = AltitudeLayer.Pawn.AltitudeFor();

                //Graphics.DrawMesh(mesh, drawPos + Graph.graphicData.drawOffset.RotatedBy(Rotation), Quaternion.identity, Graph.graphicData.Graphic.MatAt(Rotation), 0);
                // Graphics.DrawMesh(mesh, drawPos + Graph.graphicData.drawOffset.RotatedBy(Rotation), this.Rotation.AsQuat, Graph.graphicData.Graphic.MatAt(Rotation), 0);
            }
            if (IsChange != Drafted)
            {
                this.Rotation = Rot4.South;
                //rotationTracker.UpdateRotation();
            }
            IsChange = Drafted;
        }
        //base.DrawAt(drawLoc, flip);

    }
}
